package com.mcxx.modules.tool.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.mcxx.common.ExternalFileUtil;
import com.mcxx.modules.external.guicai.entity.SHJZT204Y19Entity;
import com.mcxx.modules.external.guicai.entity.SHJZT205Y19Entity;
import com.mcxx.modules.external.guicai.service.write.SHJZT204Y19WriteService;
import com.mcxx.modules.external.guicai.service.write.SHJZT205Y19WriteService;
import com.mcxx.modules.miniSalvation.family.dto.FamilyMemberDTO;
import com.mcxx.modules.miniSalvation.family.entity.BankEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMainHistoryEntity;
import com.mcxx.modules.miniSalvation.family.service.read.BankReadService;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMainHistoryReadService;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMemberHistoryReadService;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundPayFamilyDTO;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayEntity;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayFamilyReadService;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.order.entity.StandardEntity;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.miniSalvation.order.service.read.StandardReadService;
import com.mcxx.modules.miniSalvation.salary.entity.MemberSalaryEntity;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilyMemberSalaryReadService;
import com.mcxx.modules.statistics.entity.StatisticsExportEntity;
import com.mcxx.modules.system.param.StandardParam;
import com.mcxx.modules.system.service.read.StatisticsExportReadService;
import com.mcxx.modules.system.service.write.StatisticsExportWriteService;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Service
@TaTransactional
public class GuicaiSubsistenceService {

    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(GuicaiSubsistenceService.class);

    @Autowired
    private FundPayFamilyReadService fundPayFamilyReadService;
    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private FamilyMainHistoryReadService familyMainHistoryReadService;
    @Autowired
    private FamilyMemberSalaryReadService familyMemberSalaryReadService;
    @Autowired
    private FamilyMemberHistoryReadService familyMemberHistoryReadService;
    @Autowired
    private StandardReadService standardReadService;
    @Autowired
    private BankReadService bankReadService;
    @Autowired
    private SHJZT204Y19WriteService shJZT204Y19WriteService;
    @Autowired
    private SHJZT205Y19WriteService shJZT205Y19WriteService;
    @Autowired
    private StatisticsExportReadService statisticsExportReadService;
    @Autowired
    private StatisticsExportWriteService statisticsExportWriteService;




    private static String donePath = "E:/gc_sb_done.txt";
    private static String errPath = "E:/gc_error.txt";

    private final RejectedExecutionHandler THREAD_HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
    private final ThreadPoolExecutor SMS_GATE_SERVE_DELIVER = new ThreadPoolExecutor(20,
            20, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(50),
            THREAD_HANDLER);

    public void findFamilyByPay(String id, String strMonths, String areaCode, String guicaiType,
                                List<?> capitalArrs,List<?> standCitySalvArr,List<?> allowanceCitySalvArr,List<?> sustomCitySalvArr){

        StandardParam p = new StandardParam();
        p.setStatus("1");
        p.setAreaCode(areaCode+"000000");
        Double standardMoney = 0.0;
        switch (guicaiType){
            case "204":
                List<FundPayFamilyDTO> salvFamiltList = new ArrayList<>();
                List<FundPayFamilyDTO> reissueFamiltList = new ArrayList<>();
                for (Object aa : capitalArrs){
                    FundPayEntity fp = (FundPayEntity) aa;
                    List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                    salvFamiltList.addAll(dtoByFundPayId);
                }
                if(CollectionUtils.isNotEmpty(standCitySalvArr)){
                    for (Object aa : standCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }
                if(CollectionUtils.isNotEmpty(allowanceCitySalvArr)){
                    for (Object aa : allowanceCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(sustomCitySalvArr)){
                    for (Object aa : sustomCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(salvFamiltList)){
                    p.setStandType("01");
                    List<StandardEntity> byParam = standardReadService.getByParam(p);
                    if(CollectionUtils.isNotEmpty(byParam)){
                        standardMoney = byParam.get(0).getStandardMoney();
                    }
                    Map<String, Double> uuidNameMap = new HashMap<>();
                    if(CollectionUtils.isNotEmpty(reissueFamiltList)){
                        uuidNameMap = reissueFamiltList.stream().collect(Collectors.toMap(FundPayFamilyDTO::getOrderId, FundPayFamilyDTO::getFundNumber, (item1, item2) -> item1));
                    }
                    SMS_GATE_SERVE_DELIVER.execute(new executeCitySalvData(uuidNameMap,salvFamiltList,standardMoney,areaCode,strMonths,id));
                }
            break;
            case "205":
                salvFamiltList = new ArrayList<>();
                reissueFamiltList = new ArrayList<>();
                for (Object aa : capitalArrs){
                    FundPayEntity fp = (FundPayEntity) aa;
                    List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                    salvFamiltList.addAll(dtoByFundPayId);
                }
                if(CollectionUtils.isNotEmpty(standCitySalvArr)){
                    for (Object aa : standCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }
                if(CollectionUtils.isNotEmpty(allowanceCitySalvArr)){
                    for (Object aa : allowanceCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(sustomCitySalvArr)){
                    for (Object aa : sustomCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(salvFamiltList)){
                    p.setStandType("02");
                    List<StandardEntity> byParam = standardReadService.getByParam(p);
                    if(CollectionUtils.isNotEmpty(byParam)){
                        standardMoney = byParam.get(0).getStandardMoney();
                    }
                    Map<String, Double> uuidNameMap = new HashMap<>();
                    if(CollectionUtils.isNotEmpty(reissueFamiltList)){
                        uuidNameMap = reissueFamiltList.stream().collect(Collectors.toMap(FundPayFamilyDTO::getOrderId, FundPayFamilyDTO::getFundNumber, (item1, item2) -> item1));
                    }
                    SMS_GATE_SERVE_DELIVER.execute(new executeVillSalvData(uuidNameMap,salvFamiltList,standardMoney,areaCode,strMonths,id));
                }
            break;
        }
    }

    class executeCitySalvData implements Runnable{

        private List<FundPayFamilyDTO> list;

        private Double standardMoney;

        private String areaCode;

        private String strMonths;

        private String id;

        private Map<String, Double> reissueMap;

        public executeCitySalvData(Map<String, Double> reissueMap,List<FundPayFamilyDTO> list,Double standardMoney,String areaCode,String strMonths,String id){
            this.list = list;
            this.standardMoney = standardMoney;
            this.areaCode = areaCode;
            this.strMonths = strMonths;
            this.id = id;
            this.reissueMap = reissueMap;
        }

        @Override
        public void run() {
            try {
                if(CollectionUtils.isNotEmpty(list)){
                    for (FundPayFamilyDTO pf : list){
                        if(ExternalFileUtil.traverseFolder(donePath, pf.getOrderId())){
                            logger.info("oId 已处理");
                        }else{

                            if(StringUtils.isEmpty(id)){
                                StatisticsExportEntity en = new StatisticsExportEntity();
                                en.setObjectType(Constant.StatisBusinessCode.CITY_DIBAO);
                                en.setYears(strMonths);
                                en.setAreaCode(areaCode);
                                StatisticsExportEntity byYearATypeACode = statisticsExportReadService.getByYearATypeACode(en);
                                if(null == byYearATypeACode){
                                    id = Constant.getUUID();
                                    en.setId(id);
                                    en.setOrgId(areaCode+"000000");
                                    en.setExportTime(new Date());
                                    en.setStatus("0");
                                    statisticsExportWriteService.insertIgnoreNull(en);
                                }else{
                                    if(!"0".equals(byYearATypeACode.getStatus())){
                                        break;
                                    }
                                    id = byYearATypeACode.getId();
                                }
                            }
                            OrderEntity order = orderReadService.getById(pf.getOrderId());
                            if(null == order){
                                ExternalFileUtil.writeLog(errPath,"未查询到业务记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            FamilyMainHistoryEntity family = familyMainHistoryReadService.getByOrderId(pf.getOrderId());
                            if(null == family){
                                ExternalFileUtil.writeLog(errPath,"未查询到家庭历史记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            List<FamilyMemberDTO> memberList = familyMemberHistoryReadService.listByOrderId(pf.getOrderId());
                            if(CollectionUtils.isEmpty(memberList)){
                                ExternalFileUtil.writeLog(errPath,"未查询到成员历史记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            List<MemberSalaryEntity> memberSalary = familyMemberSalaryReadService.queryMemberSumSalaryByOrderId(pf.getOrderId());
                            if(CollectionUtils.isEmpty(memberSalary)){
                                ExternalFileUtil.writeLog(errPath,"未查询到成员待遇记录,orderId:"+pf.getOrderId());
                                continue;
                            }
                            FamilyMemberDTO famOwner = new FamilyMemberDTO();
                            for(FamilyMemberDTO mb : memberList){
                                if(mb.getFamilyRelation().equals("01")){
                                    famOwner=mb;
                                }
                            }

                            SHJZT204Y19Entity main = new SHJZT204Y19Entity();
                            main.setRECID(Constant.getUUID());
                            main.setORGUNIT(order.getAreaCode());
                            main.setENTERINGTIME(DateUtil.now());
                            main.setT204_DJSJ_F1_Y19(DateUtil.formatDate(order.getCommitDate()));
                            main.setSJBHSJ(DateUtil.date2Str(order.getCreateTime(),DateUtil.sdf));
                            main.setSJBHZT(order.getType().equals("01") ? "新申请" : (order.getType().equals("02") ? "复查" : "其他"));
                            main.setFamilyId(order.getFamilyId());
                            main.setOrder_id(order.getOrderId());
                            main.setT204_ZJHM_F2_Y19(family.getSalvationNo());
                            main.setT204_SFZH_F3_Y19(family.getIdcard());
                            main.setT204_HZXM_F4_Y19(family.getName());
                            main.setT204_XB_F5_Y19(famOwner.getSex());
                            main.setT204_MZ_F6_Y19(famOwner.getNation());

                            if(order.getRemark().length() > 50){
                                main.setT204_BZ_F31_Y19(order.getRemark().substring(0,50));
                            }else{
                                main.setT204_BZ_F31_Y19(order.getRemark());
                            }
                            if(family.getAddress().length() > 50){
                                main.setT204_ZZ_F7_Y19(family.getAddress().substring(0,50)); // = Server_1.GCServer.Left(this.familyHis.address, 50);
                            }else{
                                main.setT204_ZZ_F7_Y19(family.getAddress());
                            }

                            main.setT204_DH_F9_Y19(family.getPhone());
                            main.setT204_XSBZ_F10_Y19("1");
                            Integer[] cals = getNumbers204(memberList);
                            main.setT204_XSBZ_F11_Y19(cals[0]);
                            main.setT204_NX_F12_Y19(cals[1]);
                            main.setT204_CJR_F13_Y19(cals[2]);
                            main.setT204_QZZ_F61_Y19(cals[3]);
                            main.setT204_LNR_F15_Y19(cals[4]);
                            main.setT204_ZZRY_F16_Y19(cals[6]);
                            main.setT204_LHJY_F17_Y19(cals[5]);
                            main.setT204_DJSY_F18_Y19(cals[7]);
                            main.setT204_WJYT_F19_Y19(cals[8]);
                            main.setT204_WCNR_F20_Y19(cals[9]);
                            Double[] monthMoney = getMonthMoney(Constant.BusinessCode.DIBAO,memberSalary);

                            main.setT204_HYBZ_F22_Y19(monthMoney[0]);

                            main.setT204_XSDB_F23_Y19(family.getFamilyRevenue());
                            if (main.getT204_XSDB_F23_Y19() < 0)
                                main.setT204_XSDB_F23_Y19(0.0);
                            main.setT204_ZDSH_F24_Y19(standardMoney);



                            BankEntity bankByFamilyId = bankReadService.getBankByFamilyId(famOwner.getFamilyId(), famOwner.getMemberId(), order.getBusinessType());

                            if(null != bankByFamilyId){
                                main.setT204_KHYX_F27_Y19(bankByFamilyId.getMasterBankCode());
                                main.setT204_ZH_F28_Y19(bankByFamilyId.getMasterAccount());
                            }
                            Double careMoney = reissueMap.get(pf.getOrderId());
                            if(null == careMoney){
                                careMoney = 0.0;
                            }
                            main.setT204_JEE_F25_Y19(monthMoney[2]);
                            main.setT204_JES_F26_Y19(careMoney);



                            main.setT204_HZSF_F60_Y19(family.getMasterInSave());

                            int no = 0;
                            for(FamilyMemberDTO m : memberList){
                                if(m.getMemberId() != famOwner.getMemberId() &&
                                        m.getIsSaveObject().equals("1") &&
                                        m.getState().equals("01")
                                ){
                                    setFamMember204(main, m, no);
                                    no++;
                                }
                            }
                            main.setStatisticsId(id);

                            shJZT204Y19WriteService.insertIgnoreNull(main);

                            ExternalFileUtil.writeLog(donePath,pf.getOrderId());
                        }
                    }
                    statisticsExportWriteService.updateStatusById(id,"1");
                }
            } catch (Exception e) {
                e.printStackTrace();
                ExternalFileUtil.writeLog(errPath,e.getMessage());
            }
        }
    }

    class executeVillSalvData implements Runnable{

        private List<FundPayFamilyDTO> list;

        private Double standardMoney;

        private String areaCode;

        private String strMonths;

        private String id;

        private Map<String, Double> reissueMap;

        public executeVillSalvData(Map<String, Double> reissueMap,List<FundPayFamilyDTO> list,Double standardMoney,String areaCode,String strMonths,String id){
            this.list = list;
            this.standardMoney = standardMoney;
            this.areaCode = areaCode;
            this.strMonths = strMonths;
            this.id = id;
            this.reissueMap = reissueMap;
        }

        @Override
        public void run() {
            try {
                if(CollectionUtils.isNotEmpty(list)){
                    for (FundPayFamilyDTO pf : list){
                        if(ExternalFileUtil.traverseFolder(donePath, pf.getOrderId())){
                            logger.info("oId 已处理");
                        }else{

                            if(StringUtils.isEmpty(id)){
                                StatisticsExportEntity en = new StatisticsExportEntity();
                                en.setObjectType(Constant.StatisBusinessCode.COUNTY_DIBAO);
                                en.setYears(strMonths);
                                en.setAreaCode(areaCode);
                                StatisticsExportEntity byYearATypeACode = statisticsExportReadService.getByYearATypeACode(en);
                                if(null == byYearATypeACode){
                                    id = Constant.getUUID();
                                    en.setId(id);
                                    en.setOrgId(areaCode+"000000");
                                    en.setExportTime(new Date());
                                    en.setStatus("0");
                                    statisticsExportWriteService.insertIgnoreNull(en);
                                }else{
                                    if(!"0".equals(byYearATypeACode.getStatus())){
                                        break;
                                    }
                                    id = byYearATypeACode.getId();
                                }
                            }
                            OrderEntity order = orderReadService.getById(pf.getOrderId());
                            if(null == order){
                                ExternalFileUtil.writeLog(errPath,"未查询到业务记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            FamilyMainHistoryEntity family = familyMainHistoryReadService.getByOrderId(pf.getOrderId());
                            if(null == family){
                                ExternalFileUtil.writeLog(errPath,"未查询到家庭历史记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            List<FamilyMemberDTO> memberList = familyMemberHistoryReadService.listByOrderId(pf.getOrderId());
                            if(CollectionUtils.isEmpty(memberList)){
                                ExternalFileUtil.writeLog(errPath,"未查询到成员历史记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            List<MemberSalaryEntity> memberSalary = familyMemberSalaryReadService.queryMemberSumSalaryByOrderId(pf.getOrderId());
                            if(CollectionUtils.isEmpty(memberSalary)){
                                ExternalFileUtil.writeLog(errPath,"未查询到成员待遇记录,orderId:"+pf.getOrderId());
                                continue;
                            }
                            FamilyMemberDTO famOwner = new FamilyMemberDTO();
                            for(FamilyMemberDTO mb : memberList){
                                if(mb.getFamilyRelation().equals("01")){
                                    famOwner=mb;
                                }
                            }

                            SHJZT205Y19Entity main = new SHJZT205Y19Entity();
                            main.setRECID(Constant.getUUID());
                            main.setORGUNIT(order.getAreaCode());
                            main.setENTERINGTIME(DateUtil.now());
                            main.setT205_DJSJ_F1_Y19(DateUtil.formatDate(order.getCommitDate()));
                            main.setSJBHSJ(DateUtil.date2Str(order.getCreateTime(),DateUtil.sdf));
                            main.setSJBHZT(order.getType().equals("01") ? "新申请" : (order.getType().equals("02") ? "复查" : "其他"));
                            main.setFamilyId(order.getFamilyId());
                            main.setOrder_id(order.getOrderId());
                            main.setT205_ZJHM_F2_Y19(family.getSalvationNo());
                            main.setT205_SFZH_F3_Y19(family.getIdcard());
                            main.setT205_HZXM_F4_Y19(family.getName());
                            main.setT205_XB_F5_Y19(famOwner.getSex());
                            main.setT205_MZ_F6_Y19(famOwner.getNation());
                            if(order.getRemark().length() > 50){
                                main.setT205_BZ_F27_Y19(order.getRemark().substring(0,50));
                            }else{
                                main.setT205_BZ_F27_Y19(order.getRemark());
                            }
                            if(family.getAddress().length() > 50){
                                main.setT205_ZZ_F7_Y19(family.getAddress().substring(0,50)); // = Server_1.GCServer.Left(this.familyHis.address, 50);
                            }else{
                                main.setT205_ZZ_F7_Y19(family.getAddress());
                            }


                            main.setT205_DH_F9_Y19(family.getPhone());

                            Integer[] cals = getNumbers205(memberList);
                            main.setT205_XSRS_F11_Y19(cals[0]);
                            main.setT205_NX_F12_Y19(cals[1]);
                            main.setT205_CJR_F15_Y19(cals[2]);
                            main.setT205_QZZ_F61_Y19(cals[3]);
                            main.setT205_LNR_F13_Y19(cals[4]);
                            main.setT205_CNR_F16_Y19(cals[5]);
                            main.setT205_YLDT_F63_Y19(cals[6]);
                            main.setT205_WLDT_F64_Y19(cals[7]);
                            main.setT205_WCNR_F14_Y19(cals[8]);
                            main.setT205_NRFP_F62_Y19(cals[9]);

                            Double[] monthMoney = getMonthMoney(Constant.BusinessCode.DIBAO,memberSalary);
                            main.setT205_YBZJ_F17_Y19(monthMoney[0]);
                            main.setT205_XSDB_F20_Y19(family.getFamilyRevenue());
                            if (main.getT205_XSDB_F20_Y19() < 0)
                                main.setT205_XSDB_F20_Y19(0.0);
                            main.setT205_ZDBZ_F21_Y19(standardMoney);



                            BankEntity bankByFamilyId = bankReadService.getBankByFamilyId(famOwner.getFamilyId(), famOwner.getMemberId(), order.getBusinessType());

                            if(null != bankByFamilyId){
                                main.setT205_KHYX_F23_Y19(bankByFamilyId.getMasterBankCode());
                                main.setT205_ZH_F24_Y19(bankByFamilyId.getMasterAccount());
                            }
                            Double careMoney = reissueMap.get(pf.getOrderId());
                            if(null == careMoney){
                                careMoney = 0.0;
                            }
                            main.setT205_JEE_F18_Y19(monthMoney[2]);
                            main.setT205_JES_F19_Y19(careMoney);



                            main.setT205_HZSF_F60_Y19(family.getMasterInSave());

                            int no = 0;
                            for(FamilyMemberDTO m : memberList){
                                if(m.getMemberId() != famOwner.getMemberId() &&
                                        m.getIsSaveObject().equals("1") &&
                                        m.getState().equals("01")
                                ){
                                    setFamMember205(main, m, no);
                                    no++;
                                }
                            }
                            main.setStatisticsId(id);

                            shJZT205Y19WriteService.insertIgnoreNull(main);

                            ExternalFileUtil.writeLog(donePath,pf.getOrderId());
                        }
                    }
                    statisticsExportWriteService.updateStatusById(id,"1");
                }
            } catch (Exception e) {
                e.printStackTrace();
                ExternalFileUtil.writeLog(errPath,e.getMessage());
            }
        }
    }

    private Integer[] getNumbers205(List<FamilyMemberDTO> list){
        Integer[] rnt = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for (int i = 0; i< list.size(); i++) {
            FamilyMemberDTO dto = list.get(i);

            if (dto.getState().equals("01") && dto.getIsSaveObject().equals("1"))
            {
                rnt[0]++;
                if (null != dto.getBirthday()) {
                    long bir = dto.getBirthday().getTime();
                    long now = System.currentTimeMillis();
                    long chang = 31536000000L;
                    long age = (now - bir) / chang;
                    if (age >= 60)
                        rnt[4]++;
                    else if (age >= 18) {
                        rnt[5]++;
                        if(!StringUtils.isEmpty(dto.getWorkPower())){
                            if (dto.getWorkPower().equals("01") || dto.getWorkPower().equals("02"))
                                rnt[6]++;
                            else
                                rnt[7]++;
                        }else {
                            rnt[7]++;
                        }
                    }
                    else {

                        rnt[8]++;
                    }
                }
                else {
                    rnt[5]++;
                }
                if (!StringUtils.isEmpty(dto.getSex()) && dto.getSex().equals("2"))
                    rnt[1]++;
                if (!StringUtils.isEmpty(dto.getDisabilityType()))
                {
                    rnt[2]++;
                    int disLevel = 100;
                    if (!StringUtils.isEmpty(dto.getDisabilityLevel()))
                    {
                        if(!StringUtils.isNumber(dto.getDisabilityLevel())){
                            disLevel = 100;
                        }
                    }
                    if (dto.getDisabilityType().equals("65") || dto.getDisabilityType().equals("66")) {
                        if (disLevel <= 3)
                            rnt[3]++;
                    }
                    else {
                        if (disLevel < 3)
                            rnt[3]++;
                    }
                }
                if (!StringUtils.isEmpty(dto.getIsHelpPoor())) {
                    if(dto.getIsHelpPoor().startsWith("01")){
                        rnt[9]++;
                    }
                }
            }
        }
        return rnt;
    }


    private Integer[] getNumbers204(List<FamilyMemberDTO> list){

        Integer[] rnt = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

        for(int i = 0; i< list.size(); i++)
        {
            FamilyMemberDTO m = list.get(i);
            if (m.getState().equals("01") && m.getIsSaveObject().equals("1"))
            {
                rnt[0]++;
                if (null != m.getBirthday())
                {
                    long bir = m.getBirthday().getTime();
                    long now = System.currentTimeMillis();
                    long chang = 31536000000L;
                    long age = (now - bir) / chang;
                    if (age >= 60)
                        rnt[4]++;
                    else if (StringUtils.isEmpty(m.getWorkState()))
                        rnt[5]++;
                    else if (m.getWorkState().equals("01") && age >= 18 && age < 60)
                        rnt[6]++;
                    else if (m.getWorkState().equals("02")  && age >= 18 && age < 60)
                        rnt[7]++;
                    else if (m.getWorkState().equals("03")  && age >= 18 && age < 60)
                        rnt[8]++;
                    else if (age < 18)
                        rnt[9]++;
                    else
                        rnt[5]++;
                }
                else
                {
                    rnt[5]++;
                }
                if (!StringUtils.isEmpty(m.getSex()) && m.getSex().equals("2"))
                    rnt[1]++;
                if (!StringUtils.isEmpty(m.getDisabilityType()))
                {
                    rnt[2]++;
                    Integer disLevel = 0;
                    if (!StringUtils.isEmpty(m.getDisabilityLevel()))
                    {
                        if(!StringUtils.isNumber(m.getDisabilityLevel())){
                            disLevel = 100;
                        }
                    }
                    //var disLevel = m.Disability_level == null ? 100 : Convert.ToInt16(m.Disability_level);
                    if (m.getDisabilityType().equals("65") || m.getDisabilityType().equals("66"))
                    {
                        if (disLevel <= 3)
                            rnt[3]++;
                    }
                    else
                    {
                        if(disLevel < 3)
                            rnt[3]++;
                    }
                }
            }
        }
        return rnt;
    }


    private Double[] getMonthMoney(String busiType,List<MemberSalaryEntity> list){
        Double[] rnt = {0.0,0.0,0.0};
        for(MemberSalaryEntity sa : list){
            Double salaryNumber = sa.getSalaryNumber();
            if(Constant.BusinessCode.DIBAO.equals(busiType)){
                if (sa.getSalaryType().equals("01") || sa.getSalaryType().equals("02"))
                    rnt[0] += salaryNumber;
                if (sa.getSalaryType().equals("03") || sa.getSalaryType().equals("04"))
                    rnt[2] += salaryNumber;
            }else if(Constant.BusinessCode.TEKUN.equals(busiType)){
                if(sa.getItemType().equals("310")){
                    rnt[0] += salaryNumber;
                }else{
                    rnt[1] += salaryNumber;
                }
            }
        }
        return rnt;
    }

    private void setFamMember204(SHJZT204Y19Entity ent,FamilyMemberDTO mh,int no){
        if (no == 0)
        {
            ent.setT204_JTCY_F40_Y19(mh.getName());
            ent.setT204_SFZH_F41_Y19(mh.getIdcard().toUpperCase());
        }
        if (no ==1)
        {
            ent.setT204_JTCY_F42_Y19(mh.getName());
            ent.setT204_SFZH_F43_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 2)
        {
            ent.setT204_JTCY_F44_Y19(mh.getName());
            ent.setT204_SFZH_F45_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 3)
        {
            ent.setT204_JTCY_F46_Y19(mh.getName());
            ent.setT204_SFZH_F47_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 4)
        {
            ent.setT204_JTCY_F48_Y19(mh.getName());
            ent.setT204_SFZH_F49_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 5)
        {
            ent.setT204_JTCY_F50_Y19(mh.getName());
            ent.setT204_SFZH_F51_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 6)
        {
            ent.setT204_JTCY_F52_Y19(mh.getName());
            ent.setT204_SFZH_F53_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 7)
        {
            ent.setT204_JTCY_F54_Y19(mh.getName());
            ent.setT204_SFZH_F55_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 8)
        {
            ent.setT204_JTCY_F56_Y19(mh.getName());
            ent.setT204_SFZH_F57_Y19(mh.getIdcard().toUpperCase());
        }
    }
    private void setFamMember205(SHJZT205Y19Entity ent,FamilyMemberDTO mh,int no){
        if (no == 0)
        {
            ent.setT205_JTCY_F40_Y19(mh.getName());
            ent.setT205_SFZH_F41_Y19(mh.getIdcard().toUpperCase());
        }
        if (no ==1)
        {
            ent.setT205_JTCY_F42_Y19(mh.getName());
            ent.setT205_SFZH_F43_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 2)
        {
            ent.setT205_JTCY_F44_Y19(mh.getName());
            ent.setT205_SFZH_F45_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 3)
        {
            ent.setT205_JTCY_F46_Y19(mh.getName());
            ent.setT205_SFZH_F47_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 4)
        {
            ent.setT205_JTCY_F48_Y19(mh.getName());
            ent.setT205_SFZH_F49_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 5)
        {
            ent.setT205_JTCY_F50_Y19(mh.getName());
            ent.setT205_SFZH_F51_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 6)
        {
            ent.setT205_JTCY_F52_Y19(mh.getName());
            ent.setT205_SFZH_F53_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 7)
        {
            ent.setT205_JTCY_F54_Y19(mh.getName());
            ent.setT205_SFZH_F55_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 8)
        {
            ent.setT205_JTCY_F56_Y19(mh.getName());
            ent.setT205_SFZH_F57_Y19(mh.getIdcard().toUpperCase());
        }
    }
}
